package com.itheima.service.impl;

import com.itheima.dao.UserDao;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description:
 * @author: wjx
 * @date: 2022/1/8 12:00
 */
//<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
//@Component("userService")
@Service("userService")
//@Scope("prototype")
@Scope("singleton")
public class UserServiceImpl implements UserService {

    @Value("${jdbc.driver}")
    private String driver;

    //<property name="userDao" ref="userDao"></property>

//    @Autowired // 按照数据类型从Spring容器中进行匹配的
//    @Qualifier("userDao") // 是按照ID值从容器中进行匹配的，但是注意此处@Qualifier要结合@Autowired

    @Resource(name = "userDao") // @Resource 相当于@Qualifier+@Autowired
    private UserDao userDao;

    //xml配置需要写下面set方法，如果使用注解，可以去掉
//    public void setUserDao(UserDao userDao) {
//        this.userDao = userDao;
//    }

    public void save() {
        System.out.println(driver);
        userDao.save();
        Date dNow = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String today = sdf.format(dNow);
        System.out.println(sdf);
        System.out.println(today);
    }

    @PostConstruct
    public void init() {
        System.out.println("Service对象的初始化");
    }

    @PreDestroy
    public void destory() {
        System.out.println("Service对象的销毁");
    }
}
